/**
 * group 账单组
 */
Jet().$package('app.group', function(J){
	var $=J.dom.id,
		$D=J.dom,
		$E=J.event,
		$H=J.http,
		$U=J.ui,
		$tool=J.tool;
	
	//账单组管理
	this.manageGroup = function(){
		$D.html($('data_group'), '<tr><td colspan="5"><div class="loading"></div></td></tr>');
		var uri = '/ajax.php?d=group&c=GroupProxy&f=mine';
		$tool.get(uri,function(response){
			var data = response.data;
			var sHtml = '';
			J.each(data.list,function(key,row){
				sHtml += '<tr>';
				sHtml += '<td id="g_name_'+row.g_id+'">'+row.g_name+'</td>';
				sHtml += '<td>'+row.g_quantity+'</td>';
				sHtml += '<td>'+row.g_creator_name+'</td>';
				var defaultLink = '<a href="javascript:app.group.setDefault('+row.g_id+');">设为默认</a>';
				if(row.is_default == 1) defaultLink = '默认';
				sHtml += '<td id="default'+row.g_id+'">'+defaultLink+'</td>';
				var operLink = '';
				if(row.g_creator == row.u_id) {
					operLink = '<a href="javascript:app.group.modifyGroup('+row.g_id+');">修改</a>';
					var sOnclick = 'app.group.removeGroup('+row.g_id+',this);';
					operLink += '&nbsp;&nbsp;<a href="javascript:void(0);" onclick="'+sOnclick+'">删除</a>';
				} else {
					var sOnclick = 'app.group.exitGroup('+row.g_id+',this);';
					operLink = '<a href="javascript:void(0);" onclick="'+sOnclick+'">退出群组</a>';
				}
				sHtml += '<td>'+operLink+'</td>';
				sHtml += '</tr>';
			});
			if(sHtml!='') $D.html($('data_group'), sHtml);
			else $D.html($('data_group'), '<tr><td colspan="5">暂无数据~</td></tr>');
		});
		J.ui.dialog({
			'divNode':$('group_manage'), 
			'title':'账单组管理', 
			'width':'600px',
			'height':'400px',
			'isDrag':false,
			'isLayer':true
		});
	};
	
	//设置默认分组
	this.setDefault = function(g_id){
		var uri='/ajax.php?d=group&c=GroupProxy&f=setDefault';
		$tool.post(uri,{group_id:g_id},function(response){
			if(response.result==0) {
				$tool.alertMsg(response.data);
				return;
			}else{
				var data = response.data,
					dOlder = $('default'+data.older),
					dNewer = $('default'+data.newer);
				if(dOlder) $D.html(dOlder, '<a href="javascript:app.group.setDefault('+data.older+');">设为默认</a>');
				if(dNewer) $D.text(dNewer, '是');
			}
		});
	};
	
	//添加账单组
	this.addGroup = function(){
		$tool.activeButton('btn_group_submit');
		$D.hide($('group_form_error'));
		var dGroupForm = $('group_form');
		dGroupForm['group_id'].value = '';
		dGroupForm.reset();
		J.ui.dialog({
			'divNode':$('group_edit'), 
			'title':'添加账单组', 
			'width':'250px',
			'height':'auto',
			'isDrag':false,
			'isLayer':true
		});				
	};
	
	//修改账单组
	this.modifyGroup = function(group_id){
		$tool.activeButton('btn_group_submit');
		$D.hide($('group_form_error'));
		$('group_form').reset();
		var dGroupForm = $('group_form');
		dGroupForm['group_id'].value = group_id;
		dGroupForm['group_name'].value = $D.text($('g_name_'+group_id));
		J.ui.dialog({
			'divNode':$('group_edit'), 
			'title':'修改账单组', 
			'width':'250px',
			'height':'auto',
			'isDrag':false,
			'isLayer':true
		});		
	};
	
	//提交账单组
	this.submitGroup = function(){
		$tool.disableButton('btn_group_submit');
		var dGroupForm = $('group_form');
		if(dGroupForm['group_id'].value==''){
			var uri='/ajax.php?d=group&c=GroupProxy&f=create';
			$tool.post(uri,$H.serialize(dGroupForm),function(response){
				if(response.result==0) {
					var dGroupError = $('group_form_error');
					$D.text(dGroupError, response.data);
					$D.show(dGroupError);
				}else{
					var row = response.data;
					var sHtml = '<tr>';
					sHtml += '<td id="g_name_'+row.g_id+'">'+row.g_name+'</td>';
					sHtml += '<td>'+row.g_quantity+'</td>';
					sHtml += '<td>'+row.g_creator_name+'</td>';
					var defaultLink = '<a href="javascript:app.group.setDefault('+row.g_id+');">设为默认</a>';
					if(row.is_default == 1) defaultLink = '默认';
					sHtml += '<td id="default'+row.g_id+'">'+defaultLink+'</td>';
					var operLink = '<a href="javascript:app.group.modifyGroup('+row.g_id+');">修改</a>';
					var sOnclick = 'app.group.removeGroup('+row.g_id+',this);';
					operLink += '&nbsp;&nbsp;<a href="javascript:void(0);" onclick="'+sOnclick+'">删除</a>';
					sHtml += '<td>'+operLink+'</td>';
					sHtml += '</tr>';
					$D.append($('data_group'), sHtml);
					$U.closeDialog('group_edit');
				}
				$tool.activeButton('btn_group_submit');
			});
		}else{
			var uri='/ajax.php?d=group&c=GroupProxy&f=modify';
			$tool.post(uri,$H.serialize(dGroupForm),function(response){
				if(response.result==0) {
					var dGroupError = $('group_form_error');
					$D.text(dGroupError, response.data);
					$D.show(dGroupError);
				}else{
					var row = response.data;
					$D.text($('g_name_'+row.group_id), row.group_name);
					$U.closeDialog('group_edit');
				}
				$tool.activeButton('btn_group_submit');
			});
		}		
	};
	
	//退出群组
	this.exitGroup = function(g_id,aNode){
		var context = this;
		J.tool.confirm('您确定要退出吗？', function(){
			var tdNode = aNode.parentNode;
			var ahtml = tdNode.innerHTML;
			$D.html(tdNode, '<div class="loading"></div>');
			var uri = '/ajax.php?d=group&c=GroupProxy&f=exitGroup';
			$tool.post(uri,{group_id:g_id},function(response){
				if(response.result==0) {
					$tool.alertMsg(response.data);
					$D.html(tdNode, ahtml);
				} else {
					$D.remove(tdNode.parentNode);
				}
			});
		});		
	};
	
	//删除群组
	this.removeGroup = function(g_id,aNode){
		var context = this;
		J.tool.confirm('您确定删除吗？', function(){
			var tdNode = aNode.parentNode;
			var ahtml = tdNode.innerHTML;
			$D.html(tdNode, '<div class="loading"></div>');
			var uri = '/ajax.php?d=group&c=GroupProxy&f=removeGroup';
			$tool.post(uri,{group_id:g_id},function(response){
				if(response.result==0) {
					$tool.alertMsg(response.data);
					$D.html(tdNode, ahtml);
				} else {
					$D.remove(tdNode.parentNode);
				}
			});
		});		
	};
	
});